package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dey.entity.Result;
import org.dey.mapper.FormulateSchemeMapper;
import org.dey.pojo.*;
import org.dey.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author hg
 * @date 2022/4/4
 * @classname
 */
@Service
@Transactional
public class FormulateSchemeServiceImpl extends ServiceImpl<FormulateSchemeMapper, FormulateScheme> implements FormulateSchemeService {
    //膳食
    @Autowired
    DietaryService dietaryService;
    //运动
    @Autowired
    ExerciseService exerciseService;

    //运动干预
    @Autowired
    MovmentplanService movmentplanService;

    //膳食干预
    @Autowired
    MealplanService mealplanService;

    //干预总表
    @Autowired
    FormulateSchemeMapper formulateSchemeMapper;

    @Autowired
    RenqunfeileiService renqunfeileiService;

    @Override
    public List<Exercise> exerciseqb() {
        List<Exercise> list = exerciseService.list();
        return list;
    }

    @Override
    public List<Dietary> dietaryqb() {
        List<Dietary> list = dietaryService.list();
        return list;
    }

    @Override
    public Result addganyu( int rqid,InterveneTemplate interveneTemplate) {

        int num = 0;

        Result result = null;

        //添加干预总表数据
        FormulateScheme formulateScheme = interveneTemplate.getFormulateScheme();
        boolean save = this.save(formulateScheme);
        if(save){
            num = 1;
            //添加运动干预
            Movmentplan movmentplan = interveneTemplate.getMovmentplan();
            //可以添加多条数据
            for (Exercise exercise : movmentplan.getExercises()) {
                movmentplan.setFormulateschemeid(formulateScheme.getId());
                movmentplan.setExerciseid(exercise.getId());
                boolean save1 = movmentplanService.save(movmentplan);
                if(save1){
                    num = 2;
                }
            }
            //添加膳食干预
            Mealplan mealplan = interveneTemplate.getMealplan();
            for (Dietary dietary : mealplan.getDietaries()) {
                mealplan.setFormulateschemeid(formulateScheme.getId());
                mealplan.setDietaryid(dietary.getId());
                boolean save1 = mealplanService.save(mealplan);
                if(save1){
                    num = 3;
                }
            }
        }

        if(num==3){
            //数据全部添加成功 修改数据库 人群分类表状态为已干预
            UpdateWrapper<RenQunFenLei> updateWrapper = new UpdateWrapper<RenQunFenLei>();
            updateWrapper.eq("id",rqid).set("zhuangtai","已干预");
            boolean update = renqunfeileiService.update(updateWrapper);
            if(update){
                result = new Result(true,"干预成功");
            }

        }

        return result;
    }

    @Override
    public PageVo<Member> memberXx(int pageNum, int pagesize) {

        PageHelper.startPage(pageNum, pagesize);

        List<Member> list =  formulateSchemeMapper.memberiXx();

        com.github.pagehelper.PageInfo<Member> pageInfo = new PageInfo<Member>(list);

        PageVo<Member> pageVo = new PageVo<Member>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }
}
